package win.smartown.app.base;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.ViewStub;

import win.smartown.app.R;

/**
 * Created by smartown on 2017/3/18 17:10.
 * Desc:
 */
public class BaseActivity extends SuperActivity {

    private View baseView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        setupToolbar();
    }

    protected void setupToolbar() {
        Toolbar toolbar = super.findView(R.id.base_toolbar);
        String title = getIntent().getStringExtra("title");
        try {
            toolbar.setTitle(title);
        } catch (Exception e) {
            Log.e("BaseActivity", "please use JumpUtil.java to start an activity!");
        }
        setSupportActionBar(toolbar);
        toolbar.setVisibility(View.VISIBLE);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onGoBack();
            }
        });
    }

    protected final void setBaseView(@LayoutRes int id) {
        ViewStub viewStub = super.findView(R.id.base_content);
        viewStub.setLayoutResource(id);
        baseView = viewStub.inflate();
    }

    protected <T extends View> T findView(@IdRes int id) {
        return (T) baseView.findViewById(id);
    }

    protected void onGoBack() {
        finish();
    }

}
